package com.cssw.fyzb.core.dao.text;

import com.cssw.fyzb.core.model.dodb.SysText;
import com.cssw.fyzb.core.model.dodb.SysTextKind;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Auther: zzf
 * @Date: 2020/12/15 15:05
 * @Description:
 */
@Repository
public class TestKindDaoImpl implements TextKindDao {

    public List<SysText> testKindList(String kindCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("kindCode", kindCode);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM sys_text ");
        sqlParse.addSQL(" WHERE kind_code=:kindCode");
        return SysText.dao.find(sqlParse);
    }

    public SysText getSysText(String textCode) {
        return SysText.dao.findById(textCode);
    }

    /**
     * 分类列表
     */
    public Page<SysTextKind> listKind(int page, int limit){
        Map<String, Object> param = new HashMap<>();
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM sys_text_kind ");
        return SysTextKind.dao.paginate(page,limit,sqlParse);
    }

    /**
     * 新增分类
     */
    public Boolean saveKind(String kindCode,String kindName,Long sysOperId){
        SysTextKind sysTextKind = new SysTextKind();
        sysTextKind.setKindCode(kindCode);
        sysTextKind.setKindName(kindName);
        sysTextKind.setCreateOperId(sysOperId);
        sysTextKind.setCreateTime(new Date());
        return sysTextKind.save();
    }

    public Boolean updateKind(String kindCode,String kindName,Long sysOperId){
        SysTextKind sysTextKind = new SysTextKind();
        sysTextKind.setKindCode(kindCode);
        sysTextKind.setKindName(kindName);
        sysTextKind.setUpdateOperId(sysOperId);
        sysTextKind.setUpdateTime(new Date());
        return sysTextKind.update();
    }

    public Boolean deleteKind(String kindCode,Long sysOperId){
        SysTextKind sysTextKind = new SysTextKind();
        sysTextKind.setKindCode(kindCode);
        return sysTextKind.delete();
    }

    /**
     * 文章列表
     */
    public Page<SysText> list(int page, int limit){
        Map<String, Object> param = new HashMap<>();
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT text_code,text_name,kind_code,text_url,create_time,update_time FROM sys_text ORDER BY kind_code ");
        return SysText.dao.paginate(page,limit,sqlParse);
    }

    /**
     * 新增文章
     */
    public Boolean save(String textCode,String textName,String kindCode,String content,String textUrl,Long sysOperId){
        SysText sysText = new SysText();
        sysText.setTextCode(textCode);
        sysText.setTextName(textName);
        sysText.setKindCode(kindCode);
        sysText.setContent(content);
        sysText.setTextUrl(textUrl);
        sysText.setCreateOperId(sysOperId);
        sysText.setCreateTime(new Date());
        return sysText.save();
    }

    public Boolean update(String textCode,String textName,String kindCode,String content,String textUrl,Long sysOperId){
        SysText sysText = new SysText();
        sysText.setTextCode(textCode);
        sysText.setTextName(textName);
        sysText.setKindCode(kindCode);
        sysText.setContent(content);
        sysText.setTextUrl(textUrl);
        sysText.setUpdateOperId(sysOperId);
        sysText.setUpdateTime(new Date());
        return sysText.update();
    }

    public Boolean delete(String textCode,Long sysOperId){
        SysText sysText = new SysText();
        sysText.setTextCode(textCode);
        return sysText.delete();
    }
}
